package sopia.conn;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {


	            
	   private final Context context;
	   private DatabaseHelper DBHelper ;
	   private SQLiteDatabase db;
	   // Contructor
	   public DBAdapter (Context ctx){
		   this.context = ctx;
		   DBHelper = new DatabaseHelper(this.context);
	   }
	   
	  //---opens the database---
	    public DBAdapter open() throws SQLException 
	    {
	        db = DBHelper.getWritableDatabase();
	        return this;
	    }

	    //---closes the database---    
	    public void close() 
	    {
	        DBHelper.close();
	    }
	    
	    //---insert a account into the database---
	    public long insertAccount(String name, String amount) 
	    {
	        ContentValues initialValues = new ContentValues();
	        initialValues.put(SQLQuery.KEY_NAME_ACCOUNT, name);
	        initialValues.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
	        return db.insert(SQLQuery.DB_ACCOUNT_TABLE, null, initialValues);
	    }

	    //---deletes a particular account---
	    public boolean deleteAccount(String nameAccount) 
	    {
	        return db.delete(SQLQuery.DB_ACCOUNT_TABLE, SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, null) > 0;
	    }

	    //---retrieves all the accounts---
	    public Cursor getAllAccount() 
	    {
	        return db.query(SQLQuery.DB_ACCOUNT_TABLE, new String[] {
	        		SQLQuery.KEY_NAME_ACCOUNT,
	        		SQLQuery.KEY_AMOUNT_ACCOUNT}, 
	                null, 
	                null, 
	                null, 
	                null, 
	                null);
	    }

	    //---retrieves a particular account---
	    public Cursor getAccount(String nameAccount) throws SQLException 
	    {
	        Cursor mCursor =
	                db.query(true, SQLQuery.DB_ACCOUNT_TABLE, new String[] {
	                		SQLQuery.KEY_NAME_ACCOUNT, 
	                		SQLQuery.KEY_AMOUNT_ACCOUNT}, 
	                		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, 
	                		null,
	                		null, 
	                		null, 
	                		null, 
	                		null);
	        if (mCursor != null) {
	            mCursor.moveToFirst();
	        }
	        return mCursor;
	    }

	    //---updates a account---
	    public boolean updateAccount( String nameAccount, 
	    String amount) 
	    {
	        ContentValues args = new ContentValues();
	        args.put(SQLQuery.KEY_AMOUNT_ACCOUNT, amount);
	        return db.update(SQLQuery.DB_ACCOUNT_TABLE, args, 
	        		SQLQuery.KEY_NAME_ACCOUNT + " = " + nameAccount, null) > 0;
	    }
	    
	    
        private static class DatabaseHelper extends SQLiteOpenHelper 
        {
            DatabaseHelper(Context context) 
            {
                super(context, SQLQuery.DATABASE_NAME, null, SQLQuery.DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) 
            {
                db.execSQL(SQLQuery.SQL_CREATE_ACCOUNT_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_EXPENSIVE_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_CATEGORY_TABLE);
                db.execSQL(SQLQuery.SQL_CREATE_ITEM_EXPENSIVE_TABLE);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                                  int newVersion) 
            {
                Log.w(SQLQuery.TAG, "Upgrading database from version " + oldVersion 
                      + " to "
                      + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_ACCOUNT_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_EXPENSIVE_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_CATEG0RY_TABLE);
                db.execSQL("DROP TABLE IF EXISTS " + SQLQuery.DB_ITEM_EXPENSIVE_TABLE);
                onCreate(db);
            }
        }    
}
